package com.pescamillam.kiosco.printer;

import java.util.ArrayList;
import java.util.Arrays;

import com.pescamillam.kiosco.sql.Code;
import com.starmicronics.stario.StarIOPort;
import com.starmicronics.stario.StarIOPortException;

public class Printer {
	
	static String portName = "USBPRN:Star TUP500 Presenter (TUP592)";
	static final int TIMEOUT = 1000;
	

    /*
     * Print Sample Receipt
     *
     * This is a great block of code to get right into printing a professional receipt.
     * Explore the different commands and how each command can set the printer to format your text accordingly.
     * 
     */
    public static void print(Code code) {//GEN-FIRST:event_btnPrintSampleRActionPerformed

        StarIOPort port = null;
        String portName = Printer.portName;
        String portSettings = "";
        String status = "";

        try {
            port = StarIOPort.getPort(portName, portSettings, TIMEOUT);

            byte[] data;
            ArrayList<Byte> list = new ArrayList<Byte>();

            Byte[] tempList;
          
            list.addAll(Arrays.asList(new Byte[]{0x1b, 0x1D, 0x61, 0x01}));
            list.addAll(Arrays.asList(new Byte[]{0x06, 0x09, 0x1b, 0x68, 0x01, 0x01}));

            list.addAll(Arrays.asList(new Byte[]{0x1b, 0x1c, 0x70, 0x01, 0x00, '\r', '\n'}));  //Stored Logo Printing
            
            list.addAll(Arrays.asList(new Byte[]{0x1b, 0x69, 0x00, 0x00}));
            list.addAll(Arrays.asList(new Byte[]{0x1b, 0x1D, 0x61, 0x01}));
            list.addAll(Arrays.asList(new Byte[]{0x06, 0x09, 0x1b, 0x68, 0x01, 0x01}));
            
            data = ("B I E N V E N I D O" + "\r\n").getBytes();
            tempList = new Byte[data.length];
            CopyArray(data, tempList);
            list.addAll(Arrays.asList(tempList));
            
            list.addAll(Arrays.asList(new Byte[]{0x1b, 0x69, 0x01, 0x00}));
            list.addAll(Arrays.asList(new Byte[]{0x06, 0x09, 0x1b, 0x68, 0x01, 0x01}));
            list.addAll(Arrays.asList(new Byte[]{0x1b, 0x1D, 0x61, 0x01}));
            
            data = (code.nombreUsuario + "\r\n").getBytes();
            tempList = new Byte[data.length];
            CopyArray(data, tempList);
            list.addAll(Arrays.asList(tempList));

            list.addAll(Arrays.asList(new Byte[]{0x1b, 0x69, 0x00, 0x00}));
            list.addAll(Arrays.asList(new Byte[]{0x06, 0x09, 0x1b, 0x68, 0x00, 0x01}));
            
            data = (code.cedulaUsuario + "\r\n").getBytes();
            tempList = new Byte[data.length];
            CopyArray(data, tempList);
            list.addAll(Arrays.asList(tempList));
            
            data = (code.fechaIngreso + "\r\n").getBytes();
            tempList = new Byte[data.length];
            CopyArray(data, tempList);
            list.addAll(Arrays.asList(tempList));
            
            data = (code.dispositivo + "\r\n").getBytes();
            tempList = new Byte[data.length];
            CopyArray(data, tempList);
            list.addAll(Arrays.asList(tempList));

            data = (code.marca + "\r\n").getBytes();
            tempList = new Byte[data.length];
            CopyArray(data, tempList);
            list.addAll(Arrays.asList(tempList));

            list.addAll(Arrays.asList(new Byte[]{0x1b, 0x1d, 0x61, 0x00})); // Alignment

            list.addAll(Arrays.asList(new Byte[]{0x1b, 0x44, 0x02, 0x10, 0x22, 0x00})); //Set horizontal tab

            //1d barcode example 
            list.addAll(Arrays.asList(new Byte[]{0x1b, 0x1d, 0x61, 0x01}));
            list.addAll(Arrays.asList(new Byte[]{0x1b, 0x62, 0x06, 0x02, 0x02}));
            

            data = (" " + code.codigo + "\u001e\r\n").getBytes();
            tempList = new Byte[data.length];
            CopyArray(data, tempList);
            list.addAll(Arrays.asList(tempList));

            list.addAll(Arrays.asList(new Byte[]{0x1b, 0x64, 0x02}));      //Cut
            list.addAll(Arrays.asList(new Byte[]{0x07})); // Kick cash drawer

            data = new byte[list.size()];

            for (int index = 0; index < data.length; index++) {
                data[index] = (Byte) list.get(index);
            }

            //For GUI purposes to show you hex or ascii command sent
//            logIt(data);

            int totalSizeCommunicated = WritePortHelper(port, data); // see below
            if (totalSizeCommunicated == data.length) {
                status += "Success";
            } else {
                status += "Error\nCould not write all \ndata to the printer.";
            }
        } catch (StarIOPortException e) {
            status += "Failure to print sample\nCheck printer connection.";
             e  .printStackTrace();
        } finally {
            if (port != null) {
                StarIOPort.releasePort(port);
            }
        }

        System.out.println(status);
    }//GEN-LAST:event_btnPrintSampleRActionPerformed
    
    /*
     * Helper function to cupy one byte array to another different class of byte array
     */
    private static void CopyArray(byte[] array1, Byte[] array2) {
        for (int index = 0; index < array2.length; index++) {
            array2[index] = array1[index];
        }
    }
    
    /*
     * A port helper function to send data to the printer and receive how many bytes were sent successfully
     */
    private static int WritePortHelper(StarIOPort port, byte[] writeBuffer) throws StarIOPortException {
        int zeroProgressOccurances = 0;
        int totalSizeCommunicated = 0;

        while ((totalSizeCommunicated < writeBuffer.length) && (zeroProgressOccurances < 2)) // adjust zeroProgressOccurances as needed
        {
            int sizeCommunicated = port.writePort(writeBuffer, totalSizeCommunicated, writeBuffer.length - totalSizeCommunicated);
            if (sizeCommunicated == 0) {
                zeroProgressOccurances++;
            } else {
                totalSizeCommunicated += sizeCommunicated;
                zeroProgressOccurances = 0;
            }
        }

        return totalSizeCommunicated;
    }
}
